<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of article
 *
 * @author root
 */
class article extends admintemp {
    
    public $dba;
    
    public function __construct() {
        parent::__construct();
        if(!isset($_SESSION['authpd'])){
            redirect(WEBPATH);
        }
        
        $this->dba=new db();
    }
    
    public function index(){
        $data = $this->dba->fetch_all("select * from article");
        $this->assign("contentdata", $data);
        $this->display("include/article_index.html");
    }
    
    public function edit(){
        if ($_POST['click']){
            $id = $_REQUEST['content_id'];
            $title=chulihtml($_POST['contenttitle']);
            $contentaa=htmlentities($_POST['myeditoraa'], ENT_QUOTES, "UTF-8");
            $channel_type_id = $_POST['channel_type'];
            $channel_type_name=$this->dba->fetch_arrone("select id,name from channel where id=\"$channel_type_id\"");
            $channel_type_name=$channel_type_name['name'];
            if($id=='0'){
                $sql = "insert into article (`title`,`content`,`date`,`channel`,`channel_name`) values (\"$title\",\"$contentaa\",SYSDATE(),\"$channel_type_id\",\"$channel_type_name\")";
            }else{
                $sql = "update article set `title`=\"$title\",`content`=\"$contentaa\",`channel`=\"$channel_type_id\",`channel_name`=\"$channel_type_name\" where id=\"$id\"";
            }
            if($this->dba->query($sql)){
                redirect("?c=article");
            }
        }else{
            $id = $_REQUEST['id'];
            if(empty ($id)){
                $title = '在这里输入标题...';
                $contentaa = '这里写入内容';
                $content_id = '0';
            }else{
                $data=$this->dba->fetch_arrone("select * from article where id=\"$id\"");
                $title=html_entity_decode($data['title'], ENT_QUOTES, "UTF-8");
                $contentaa = html_entity_decode($data['content'], ENT_QUOTES, "UTF-8");
                $content_id = $data['id'];
                
                $current_channel_id=$data['channel'];
                $current_channel_name=$data['channel_name'];
                $current_channel_data="<option value=\"$current_channel_id\">".$current_channel_name."</option>";
                $this->assign("current_channel_id", $current_channel_data);
            }
            $this->assign("contenttitle",$title);
            $this->assign("content_id",$content_id);
            $this->assign("contentaaa", $contentaa);
            
            $channel_type_data=$this->dba->fetch_all("select * from channel where type=\"channel\"");  
            $this->assign("row_channel_type", $channel_type_data);
            $this->display("include/article_edit.html");
        }
    }
    
    public function delarticle(){
        $id=$_REQUEST['id'];
        $sql = "delete from article where id=\"$id\"";
        if($this->dba->query($sql)){
            redirect("?c=article");
        }
    }
}

?>
